import { order, orderCreate } from "../../utils/interface";
Page({
  data: {
    orderlist: [],
    sum: 0,
  },
  onLoad(options) {
    // console.log(options);
    this.setData({
      sum: (options.sum - 0).toFixed(2),
    });
  },
  onShow() {
    this.setData({
      orderlist: wx.getStorageSync("newcart") || [],
    });
  },
  // 生成未支付订单，并从购物车中删除与订单相关的商品
  async onSubmit() {
    //获取token
    let token = wx.getStorageSync("token");
    //获取购物车
    let newCart = wx.getStorageSync("newcart") || [];
    //整理数据格式
    let goodsJsonStr = newCart.map((item) => {
      return {
        goodsId: item.goodsId,
        propertyChildIds: item.propsId,
        number: item.count,
      };
    });
    goodsJsonStr = JSON.stringify(goodsJsonStr);

    //创建未支付订单
    let { data: res } = await orderCreate({ token, goodsJsonStr }).catch(
      (err) => err
    );
    //删除购物车中订单的数据
    let cart = wx.getStorageSync("cart") || [];
    cart = cart.filter((item) => {
      return item.checked == false;
    });
    wx.setStorageSync("cart", cart);

    //跳转支付确认页
    let { amount, orderNumber } = res.data;
    wx.navigateTo({
      url:
        "/pages/payment/payment?amount=" +
        amount +
        "&orderNumber=" +
        orderNumber,
    });
  },
});
